package com.des1gn.kafka;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.common.utils.AppInfoParser;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

/**
 * @ClassName KafkaAutoConfiguration
 * @Description kafka集成自动装配
 * @Author Des1gn
 * @Version 1.0
 **/
@Configuration
@ConditionalOnClass({DefaultKafkaProducerFactory.class})
@Slf4j
public class KafkaAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean({KafkaTemplate.class, AppInfoParser.AppInfo.class})
    @ConditionalOnProperty(prefix = "spring.kafka",name = "bootstrap-servers")
    public KafkaTemplate<String,String> kafkaTemplate(ProducerFactory kafkaProducerFactory){
        log.info("kafka autoConfiguration success");
        return new KafkaTemplate<>(kafkaProducerFactory);
    }
}
